package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"share-chat/api/request"
	"share-chat/internal/model"
	"share-chat/pkg"
)

func RegisterUser(c *gin.Context) {
	var req request.RegisterUserForm
	var user model.User

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	user.Mobile = req.Mobile
	err := user.LoginUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	if user.Id > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "手机号已被使用",
			"data":    "",
		})
		return
	}

	user.Password = pkg.Md5(req.Password)

	err = user.RegisterUser()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "注册成功",
		"data":    "",
	})

}

func LoginUser(c *gin.Context) {
	c.JSON(200, gin.H{
		"message": "pong",
	})
}
